#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
using ll = long long;
const int N = 500010;
int a[N], b[N];
int n;
ll merge_sort(int a[], int l, int r)
{
    if (l >= r) return 0;
    int mid = l + r >> 1;
    ll res = merge_sort(a, l, mid) + merge_sort(a, mid + 1, r);
    int i = l, j = mid + 1, k = 0;
    while (i <= mid && j <= r)
    {
        if (a[i] <= a[j]) b[k++] = a[i++];
        else
        {
            b[k++] = a[j++];
            res += mid - i + 1;
        }
    }
    while (i <= mid) b[k++] = a[i++];
    while (j <= r) b[k++] = a[j++];
    for (int i = l, j = 0; i <= r;) a[i++] = b[j++];
    return res;
}
int main()
{
    while (cin >> n, n)
    {
        for (int i = 0; i < n; i++) cin >> a[i];
        ll res = merge_sort(a, 0, n - 1);
        cout << res << endl;
    }
    return 0;
}